package com.meituan.verintreport.task;

import com.meituan.verintreport.db.dao.AssesmentsOnFormMapper;
import com.meituan.verintreport.db.dto.AssesmentsOnFormDto;
import com.meituan.verintreport.db.verint.VerintBaseTableDao;
import com.meituan.verintreport.util.DateUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * 同步29天以内的数据，在第二天保留第2天的数据，再新建20天的数据，日期一致的数据，会呗最新的数据覆盖
 */
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "base-table.assesments-on-form")
public class BaseTableTaskAssesmentsOnForm extends BaseTableTask {
    @Resource
    private AssesmentsOnFormMapper assesmentsOnFormMapper;

    @Resource
    private VerintBaseTableDao verintBaseTableDao;

    @Value("${report.timezone}")
    private String reportTimeZone;

    @Value("${base-table.assesments-on-form.maxMinutesPerTime}")
    private int maxMinutesPerTime = 15;


    @Scheduled(cron = "${base-table.assesments-on-form.cron}")
    public void execute() {
        if (!isEnable()) {
            return;
        }

        // long startTime = System.currentTimeMillis();
        // Calendar beforeTime1 = Calendar.getInstance();
        // TimeZone timezone = TimeZone.getTimeZone(reportTimeZone);
        // Calendar beforeTime = Calendar.getInstance();
        // beforeTime.setTimeZone(timezone);
        // beforeTime.add(Calendar.HOUR_OF_DAY, -2);// 1小时// 之前的时间
        // beforeTime.set(Calendar.MINUTE, 0);
        // beforeTime.set(Calendar.SECOND, 0);
        // beforeTime.set(Calendar.MILLISECOND, 0);
        // Date beginTime = beforeTime.getTime();
        //
        // beforeTime1.setTimeZone(timezone);
        // beforeTime1.add(Calendar.HOUR_OF_DAY, -1);
        // beforeTime1.set(Calendar.MINUTE, 0);
        // beforeTime1.set(Calendar.SECOND, 0);
        // beforeTime1.set(Calendar.MILLISECOND, 0);
        // Date endDateTime = beforeTime1.getTime();

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //sdf.setTimeZone(timezone);

        //1 删除
        String start = DateUtil.getDate(21, null);
        String end = DateUtil.getDate(2,null);
        log.info("remove start:{},end:{}", start, end);
        Integer rs = assesmentsOnFormMapper.removeByTime(start, end);
        log.info("remove {} data ", rs);

       // String startTimeStr = sdf.format(beginTime) + ".000";
       // String endTimeStr = sdf.format(endDateTime) + ".000";
        //log.info("timezone = {} startTime = {} endTime = {}", timezone.getDisplayName(), startTimeStr, endTimeStr);

        //Long maxId = assesmentsOnFormMapper.getMaxIdBeforeDateTime(endTimeStr);
       // if (maxId == null) {
       //     startTimeStr = null;
       // }
       // log.info("geting records before: {}, maxId: {}", endTimeStr, maxId);

        int pageIndex = 0;
        int pageSize = 1000;
        boolean shouldContinue;
        do {
            //2 查询
            shouldContinue = false;
            String start2 = DateUtil.getDate(21, null);
            String end2 = DateUtil.getDate(1,null);
            log.info("got start:{},end:{}", start2, end2);
            List<AssesmentsOnFormDto> records = verintBaseTableDao.getAssesmentsOnFormListBeforeDateTimePaged(
                    start2, end2, pageIndex, pageSize);
            log.info("got {} records", records.size());
            if (records.size() == pageSize) {
                shouldContinue = true;
            }

            //3 insert
            for (AssesmentsOnFormDto record : records) {
                try {
                    assesmentsOnFormMapper.insert(record);
                } catch (Exception ex) {
                    log.error("Add record to db exception.", ex);
                }
            }
            pageIndex += pageSize;
        }
        while (shouldContinue);
    }
}
